/******************************************************************************/
/*Files to Include                                                            */
/******************************************************************************/

#include <xc.h>            /* HiTech General Includes */
#include <stdint.h>         /* For uint8_t definition */
#include <stdbool.h>        /* For true/false definition */

#include "define.h"

/******************************************************************************/
/* Interrupt Routines                                                         */
/******************************************************************************/
bool	t100hz;

static int_fast8_t	ledTimer;
static int_fast8_t	ledDuty[3];
static bool			led[3];
static int_fast8_t  t100hz_count;

void intInit(void)
{
	ledTimer = 3;
	led[0] = led[1] = led[2] = true;
	ledDuty[0] = ledDuty[1] = ledDuty[2] = 0;
	t100hz = false;
	t100hz_count = 80;
}

void setColor(int g, int r, int b)
{
	ledDuty[0] = b;
	ledDuty[1] = r;
	ledDuty[2] = g;
}

void interrupt isr(void)
{
    /* This code stub shows general interrupt handling.  Note that these
    conditional statements are not handled within 3 seperate if blocks.
    Do not use a seperate if block for each interrupt flag to avoid run
    time errors. */

	if(TMR2IF)
	{
		TMR2IF = 0;

		TRISC <<= 1;
		TRISC |= 0x01;
		if(TRISC == 0xFF) TRISC = 0xF7;
	}

	if(T0IF)
	{
		T0IF = 0;

		if(--ledTimer == 0)
		{
			ledTimer = 3;
			led[0] = true;
			led[1] = true;
			led[2] = true;

		}
		if(ledTimer == ledDuty[0])
		{
			led[0] = false;
		}
		if(ledTimer == ledDuty[1])
		{
			led[1] = false;
		}
		if(ledTimer == ledDuty[2])
		{
			led[2] = false;
		}
		LED_SEG_B = led[0];
		LED_SEG_R = led[1];
		LED_SEG_G = led[2];

		if(--t100hz_count == 0)
		{
			t100hz = true;
			t100hz_count = 80;
		}
	}

#if 0
    
    /* TODO Add High Priority interrupt routine code here. */

    /* Determine which flag generated the interrupt */
    if(<Interrupt Flag 1>)
    {
        <Interrupt Flag 1=0>; /* Clear Interrupt Flag 1 */
    }
    else if (<Interrupt Flag 2>)
    {
        <Interrupt Flag 2=0>; /* Clear Interrupt Flag 2 */
    }
    else
    {
        /* Unhandled interrupts */
    }

#endif

}
